// pages/blindDetail/blindDetail.js
var app = getApp();
var main = require('../../utils/main.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    imageUrl: app.globalData.imageUrl,
    isCollect:false,
    isHomepage:true,
    isShow:{
      mateCondition:true,
      basicInfo:true,
      archives:true
    },
    replyList:[{}],
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: true,
    reply: '',
    isAdd: true,
    inputFocus: false,
    replyObj:'',
    userInfo: '',
    otherId:'',
    id:'',
    userDetail:{},
    degreeObj: {'0': '不限', '1': '博士后', '2': '博士 ', '3': '硕士', '4': '本科', '5': '专科', '6': '高中', '7':'高中以下'},
    marriageObj: { '0': '未婚', '1': '离异', '2': '不限'},
    purchaseObj: { '0': '无', '1': '有' ,'2': '不限'},
    animalSignObj: { '1': '鼠', '2': '牛', '3': '虎', '4': '兔', '5': '龙', '6': '蛇', '7': '马', '8': '羊', '9': '猴', '10': '鸡', '11': '狗', '12':'猪'},
    constellationObj: { '1': '白羊座', '2': '金牛座', '3': '双子座', '4': '巨蟹座', '5': '狮子座', '6': '处女座', '7': '天秤座', '8': '天蝎座', '9': '射手座', '10': '摩羯座', '11': '水瓶座', '12':'双鱼座'},
    bloodObj: { '1': 'A型', '2': 'B型', '3': 'AB型', '4': 'O型', '5':'其他'},
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      otherId: options.userId,
      id: options.id,
    })
    main.changeInput.apply(this, []);
    if (options.isLeave){
      this.setData({
        isHomepage:false,
        // inputFocus:true
      })
    }
    wx.showLoading({})
    this.getDetail();
    this.getLeaveMes();
  },
  showInfo:function(e){//展开收起信息详情
    var value = e.currentTarget.dataset.value;
    this.setData({
      ['isShow.' + value]: !this.data.isShow[value],
    })
  },
  getDetail:function(){//获取用户详情
    var that=this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqUserBasicInfo/getOne', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        userId: this.data.userInfo.id,
        otherId: this.data.otherId,
        id:this.data.id
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (res.data.data.photos){
            res.data.data.photos = res.data.data.basicInfo.photos.split(',');
          }else{
            res.data.data.basicInfo.photo = [];
          } 
          that.setData({
            userDetail: res.data.data
          })
          if (res.data.data.criteria){
            that.relaceValue('age');
            that.relaceValue('height');
            that.relaceValue('monthIncome');
          }
        }
      }
    })
  },
  relaceValue: function (value) {
    if (this.data.userDetail.criteria[value].indexOf('-') >= 0) {
      var data = this.data.userDetail.criteria[value].split('-');
      this.setData({
        ['userDetail.criteria.' + value]: (data[0] == '0' ? '不限' : data[0]) + '-' + (data[1] == '0' ? '不限' : data[1])
      })
    } else {
      this.setData({
        ['userDetail.criteria.' + value]: '不限'
      })
    }
  },
  getLeaveMes(flag) {//获取留言数据
    var _this = this;
    var param = { type: 1, infoId: this.data.id };
    param.userId = this.data.userInfo.id;
    param.otherSideId = this.data.otherId;
    wx.request({
      url: app.globalData.httpApiCar + 'messageInfo/all', //仅为示例，并非真实的接口地址
      data: param,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
        var obj = {};
        res.data.data.forEach(function (ele) {
          ele.time = '';
          if (ele.createTime) {
            ele.time = util.getHours(ele.createTime);
          }
          if (ele.messageInfos.length > 0) {
            ele.messageInfos.forEach(function (item) {
              if (item.createTime) {
                item.time = util.getHours(item.createTime);
              }
            })

          }
        })
        _this.setData({
          replyList: res.data.data
        })
      }
    })
  },
  addLeaveMes() {//留言发送按钮点击事件
    if (this.data.reply != '') {
      var formData = {
        userId: this.data.userInfo.id,
        infoId: this.data.id,
        message: this.data.reply,
        otherSideId: this.data.otherId,
        status: 7,
        backupA: this.data.otherId
      };
      if (this.data.isAdd) {
        formData.type = 1;
      } else {
        formData.otherSideId = this.data.replyObj.userId;
        formData.type = 2;
        if (this.data.replyObj.parentId) {
          formData.parentId = this.data.replyObj.parentId;
        } else {
          formData.parentId = this.data.replyObj.id;
        }
      }
      var that = this;
      wx.request({
        url: app.globalData.httpApiCar + 'messageInfo/save', //仅为示例，并非真实的接口地址
        data: formData,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
          // 'content-type': 'multipart/form-data' // 默认值
        },
        success: function (res) {
          if (res.data.success) {
            that.setData({
              page: 1,
              reply: '',
              isAdd: true
            })
            that.getLeaveMes(true);
          } 
          else {
            wx.showToast({
              title: res.data.error,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  },
  inputBlur:function(e){
    this.setData({
      isAdd: true,
      reply: ''
    })
  },
  reply: function (e) {//回复点击事件
    this.data.replyObj = e.currentTarget.dataset.item;
    this.data.replyObj.index = e.currentTarget.dataset.index;
    this.setData({
      isAdd: false,
      inputFocus: true,
      replyObj: this.data.replyObj,
      reply: ''
    })
  },
  collect:function(e){//关注
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqUserBasicInfo/addOrCancelAttention', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        userId: this.data.userInfo.id,
        otherId: this.data.otherId,
        id: this.data.id
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            'userDetail.attentionFlag': that.data.userDetail.attentionFlag=='Y'?'N':'Y'
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
    this.setData({
      isCollect:true
    })
  },
  changeCover:function(e){
  },
  tabSwitch:function(e){//tab标签切换
    var flag=true;
    if(e.target.id=='1'){
      flag=true;
    }else{
      flag = false;
    }
    this.setData({
      isHomepage: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})